package com.apestech.framework.db.session;

import com.apestech.formwork.stream.ListStream;
import com.apestech.framework.db.SqlSession;
import com.apestech.framework.db.SqlTemplate;
import com.apestech.framework.db.callback.QueryCallBackHandler;
import com.apestech.framework.db.queryData.QueryIsEmpty;
import com.apestech.framework.db.queryData.QueryListStream;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.sql.SqlParam;
import com.apestech.framework.util.ExceptionUtil;
import com.apestech.framework.xml.plugin.modul.QryConfig;

/**
 * @author hup
 * @create 2018-08-31-11:57
 */
public class ForResultSet extends SessionUtil{

    private SqlTemplate cn;
    private QryConfig qryConfig;
    private SqlSession session;

    public ForResultSet(SqlTemplate cn, SqlSession session){
        this.cn=cn;
        this.session=session;
    }

    public void setQryConfig(QryConfig qryConfig) {
        this.qryConfig = qryConfig;
    }

    public void queryForResultSet(String sql, QueryCallBackHandler queryCallBackHandler) throws Exception{
        System.out.println(sql+";");
        try {
            this.cn.query(sql, null, queryCallBackHandler,  null, session.getSpecialField().getTimestamp());
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    public void queryForResultSet(JSONObject param, QueryCallBackHandler queryCallBackHandler) throws Exception {
        ResultQuery rsqry=new ResultQuery(cn, setParam(param));
        rsqry.queryForResultSet(queryCallBackHandler);
    }

    public void queryId(String xmlId, JSONObject param, QueryCallBackHandler queryCallBackHandler) throws Exception {
        param=setQueryId(xmlId, "select", param);
        queryForResultSet(param, queryCallBackHandler);
    }

    public void queryId(String xmlId, QueryCallBackHandler queryCallBackHandler) throws Exception {
        queryId(xmlId, null, queryCallBackHandler);
    }

    public void sql(String sql, JSONObject param, QueryCallBackHandler queryCallBackHandler) throws Exception {
        param=setSql(sql, null, null, param);
        param.put("sqltype", "select");
        queryForResultSet(param, queryCallBackHandler);
    }

    public void sql(String sql, QueryCallBackHandler queryCallBackHandler) throws Exception {
        queryForResultSet(sql, queryCallBackHandler);
    }

    public void table(String tabName, JSONObject param, QueryCallBackHandler queryCallBackHandler) throws Exception {
        param=setTableSql(tabName, "select", null, null, false, param);
        queryForResultSet(param, queryCallBackHandler);
    }

    public void table(String tabName, QueryCallBackHandler queryCallBackHandler) throws Exception {
        table(tabName, null,  queryCallBackHandler);
    }

    public void tableId(String xmlId, JSONObject param, QueryCallBackHandler queryCallBackHandler) throws Exception {
        param=setTableId(xmlId, param);
        queryForResultSet(param, queryCallBackHandler);
    }

    public void tableId(String xmlId, QueryCallBackHandler queryCallBackHandler) throws Exception {
        tableId(xmlId, null, queryCallBackHandler);
    }

    public boolean queryIsEmpty(String sql, JSONObject param) throws Exception {
        QueryIsEmpty query=new QueryIsEmpty();
        try {
            if(param==null) param=new JSONObject();
            param.put("printSql","false");
            sql(sql, param, query);
            return !query.isHasdata();
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    public boolean queryIsEmpty(String sql) throws Exception {
        JSONObject param=new JSONObject();
        param.put("printSql","false");
        return queryIsEmpty(sql, null);
    }

    public boolean tableIsEmpty(String tableName) throws Exception {
        return queryIsEmpty("SELECT 1 FROM "+tableName+" WHERE 1=1 ", null);
    }

    public String tableSelectSql(String tabName) throws Exception {
        return getTableSql(tabName, "select");
    }

    public String tableInsertSql(String tabName) throws Exception {
        return getTableSql(tabName, "insert");
    }

    public String tableUpdateSql(String tabName) throws Exception {
        return getTableSql(tabName, "update");
    }

    public String tableDeleteSql(String tabName) throws Exception {
        return getTableSql(tabName, "delete");
    }

    private String getTableSql(String tabName, String sqltype) throws Exception {
        try {
            JSONObject param=setTableSql(tabName, sqltype, null, null, true, null);
            return getQueryParamSql(session, param);
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    public String getPrintSql(String nodeName, String id, JSONObject param) throws Exception {
        switch (nodeName){
            case "queryid":{
                param=setQueryId(id, "select", param);
                break;
            }
            case "tablesid":{
                param=setTablesId(id, param);
                break;
            }
            case "tableid":{
                param=setTableId(id, param);
                break;
            }
        }
        SqlParam sqlparam=setParam(param);
        return sqlparam.getPrintSql();
    }

    private SqlParam setParam(JSONObject param) throws Exception {
        try {
            return getQueryParam(session, param, qryConfig);
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    public ListStream queryForListStream(String fileName, String sql) throws Exception{
		return queryForListStream(fileName, sql, null);
	}

	public ListStream queryForListStream(String fileName, String sql, JSONObject param) throws Exception{
		return queryForListStream(fileName, sql, param, null);
	}

	public ListStream queryForListStream(String fileName, String sql, JSONObject param, String type) throws Exception{
		QueryListStream query=new QueryListStream(fileName);
		try {
			if(param==null){
				queryForResultSet(sql, query);
			} else {
				queryForResultSet(param, query);
			}

		} catch (Exception e) {
		    ExceptionUtil.exception(this, "queryForListStream_fileName_sql_param", e.getMessage());
		} finally{
			return query.getList();
		}
	}

}
